﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuDada.Core.Post.Domain;
using WuDada.Core.Persistence.Ado;
using WuDada.Core.Persistence;
using NHibernate.Criterion;
using System.Collections;
using WuDada.Core.Generic.Extension;

namespace WuDada.Core.Post.Persistence
{
    public class QuestionDao : AdoDao, IQuestionDao
    {
        public INHibernateDao NHibernateDao { get; set; }        

        /// <summary>
        /// 新增Question
        /// </summary>
        /// <param name="questionVO">被新增的Question</param>
        /// <returns>新增後的Question</returns>
        public QuestionVO CreateQuestion(QuestionVO questionVO)
        {
            NHibernateDao.Insert(questionVO);

            return questionVO;
        }

        /// <summary>
        /// 取得Question By QuestionId
        /// </summary>
        /// <param name="questionId">QuestionId</param>
        /// <returns>Question</returns>
        public QuestionVO GetQuestionById(int questionId)
        {
            return NHibernateDao.GetVOById<QuestionVO>(questionId);
        }

        /// <summary>
        /// 刪除Question
        /// </summary>
        /// <param name="questionVO">被刪除的Question</param>
        public void DeleteQuestion(QuestionVO questionVO)
        {
            NHibernateDao.Delete(questionVO);
        }

        /// <summary>
        /// 更新Question
        /// </summary>
        /// <param name="questionVO">被更新的Question</param>
        /// <returns>更新後的Question</returns>
        public QuestionVO UpdateQuestion(QuestionVO questionVO)
        {
            NHibernateDao.Update(questionVO);

            return questionVO;
        }

        /// <summary>
        /// 取得Question清單
        /// </summary>
        /// <param name="conditions">搜尋條件</param>
        /// <returns>Question清單</returns>
        public IList<QuestionVO> GetQuestionList(IDictionary<string, string> conditions)
        {
            ArrayList param = new ArrayList();
            string fromScript = "select q from QuestionVO q ";
            StringBuilder whereScript = new StringBuilder();
            return this.QueryQuestion(param, fromScript, whereScript, conditions, true).OfType<QuestionVO>().ToList();
        }

        /// <summary>
        /// 取得Question數量
        /// </summary>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public int GetQuestionCount(IDictionary<string, string> conditions)
        {
            int count = 0;
            ArrayList param = new ArrayList();
            string fromScript = "select count(q.QuestionId) from QuestionVO q ";
            StringBuilder whereScript = new StringBuilder();
            IList result = this.QueryQuestion(param, fromScript, whereScript, conditions, false);
            if (result.Count > 0)
            {
                count = Convert.ToInt32(result[0]);
            }
            return count;
        }

        private IList QueryQuestion(ArrayList param, string fromScript, StringBuilder whereScript, IDictionary<string, string> conditions, bool useOrder)
        {
            AppendQuestionKeyWord(conditions, whereScript, param);
            AppendQuestionPost(conditions, whereScript, param);
            AppendQuestionFlag(conditions, whereScript, param);

            string hql = fromScript + "where 1=1 " + whereScript;
            if (useOrder)
            {
                hql += AppendQuestionOrder(conditions);
            }

            return NHibernateDao.Query(hql, param, conditions);
        }

        private void AppendQuestionPost(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("PostId"))
            {
                whereScript.Append(" and q.Post.PostId = ? ");
                param.Add(conditions["PostId"]);
            }
        }

        private void AppendQuestionFlag(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("Flag"))
            {
                whereScript.Append(" and q.Flag = ? ");
                param.Add(conditions["Flag"]);
            }
        }

        private void AppendQuestionKeyWord(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("KeyWord"))
            {
                whereScript.Append(" and (q.Question like ? ) ");
                param.Add("%" + conditions["KeyWord"] + "%");
            }
        }

        private string AppendQuestionOrder(IDictionary<string, string> conditions)
        {
            //// 排序條件
            string order = "order by q.SortNo, q.CreatedDate desc ";
            if (conditions.IsContainsValue("Order"))
            {
                order = conditions["Order"];
            }

            return order;
        }
    }
}
